package cn.nawang.ebeim.ui.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XmlUtil {
  
  public static final String serverPath="C:\\EbeimProtect\\config.xml";
  public static void main(String args[]) {
    // getServiceInfo("C:\\Program Files\\EbeimProtect\\config.xml");
    modifySon("C:\\EbeimProtect\\config.xml", "AutoStart", "0");
    // createXml("D://config.xml");
    // modifySon("C:\\Program Files\\EbeimProtect\\config.xml");
  }

  /**
   * 
   * @author: huanghj
   * @Description: 获取服务节点的值
   * @param @param xmlPath
   * @param @return
   * @return Map
   * @date 2015年11月18日下午3:28:04
   *
   * @修改人
   * @修改日期
   */
  public static Map getServiceInfo(String xmlPath) throws Exception {
    File file = new File(xmlPath);
    if (!file.exists()) {
      createXml(xmlPath);
    }
    Map map = new HashMap();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document xmldoc = db.parse(xmlPath);
    Element root = xmldoc.getDocumentElement();
    String autoStart = root.getElementsByTagName("AutoStart").item(0).getTextContent();
    String autoUpdate = root.getElementsByTagName("AutoUpdate").item(0).getTextContent();
    String run = root.getElementsByTagName("Run").item(0).getTextContent();
    map.put("autoStart", autoStart);
    map.put("autoUpdate", autoUpdate);
    map.put("run", run);
    return map;
  }

  /**
   * 
   * @author: huanghj
   * @Description: 修改节点值
   * @param @param xmlPath
   * @param @param name
   * @param @param status
   * @param @return
   * @return boolean
   * @date 2015年11月18日下午3:27:35
   *
   * @修改人
   * @修改日期
   */
  public static boolean modifySon(String xmlPath, String name, String status) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    try {
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document xmldoc = db.parse(xmlPath);
      Element root = xmldoc.getDocumentElement();
      root.getElementsByTagName(name).item(0).setTextContent(status);
      TransformerFactory factory = TransformerFactory.newInstance();
      Transformer former = factory.newTransformer();
      former.setOutputProperty("encoding", "UTF-8");
      former.transform(new DOMSource(xmldoc), new StreamResult(new File(xmlPath)));
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  /**
   * 
   * @author: huanghj
   * @Description: 生成服务config.xml文件
   * @param @param fileName
   * @return void
   * @date 2015年11月18日下午3:50:28
   *
   * @修改人
   * @修改日期
   */
  public static void createXml(String fileName) {
    File file = new File(fileName);
    if (file.exists()) {
      file.mkdirs();
    }
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.newDocument();
      Element root = document.createElement("Root"); // 创建根节点
      document.appendChild(root);
      Element item1 = document.createElement("AutoStart");
      item1.setTextContent("1");
      root.appendChild(item1);
      Element item2 = document.createElement("AutoUpdate");
      item2.setTextContent("1");
      root.appendChild(item2);
      Element item3 = document.createElement("Run");
      item2.setTextContent("0");
      root.appendChild(item3);
      // 将DOM对象document写入到xml文件中
      TransformerFactory tf = TransformerFactory.newInstance();
      Transformer transformer = tf.newTransformer();
      DOMSource source = new DOMSource(document);
      transformer.setOutputProperty("encoding", "UTF-8");
      transformer.setOutputProperty("version", "1.0");
      PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
      StreamResult result = new StreamResult(pw);
      transformer.transform(source, result); // 关键转换
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
